package com.qf.service.impl;

import com.qf.constant.CacheConstants;
import com.qf.constant.StrategyConstants;
import com.qf.feign.CacheFeign;
import com.qf.model.StandardSubmit;
import com.qf.service.FilterChainService;
import com.qf.util.PushMsgUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/*
 *@Description:黑名单过滤策略
 *@author:刘哥
 *@Date:86180 2022/10/19 17:46
 */
@Service("black")
@Slf4j
public class BlackServiceImpl implements FilterChainService {
    @Autowired
    private CacheFeign cacheFeign;
    @Autowired
    private PushMsgUtil pushMsgUtil;
    @Override
    public boolean strategy(StandardSubmit standardSubmit) {
        log.info("[策略模块-黑名单策略] 开始执行==========");
        //1.获取目标手机号
        String mobile = standardSubmit.getDestMobile();
        //2.拼接出Redis对应得key
        String key= CacheConstants.CACHE_PREFIX_BLACK + mobile;
        //3.根据key去缓存模块中查询数据
        String value = cacheFeign.get(key);
        if (StringUtils.isEmpty(value)){
            log.info("[策略模块 - 黑名单策略] 黑名单策略执行成功！！！");
            System.out.println("发送消息到MQ！！！！");
            return true;
        }else {
            //5.如果查询的数据不为null，当前手机号属于黑名单，发送两个消息到MQ，并且return  false
            log.info("[策略模块- 黑名单策略] 黑名单策略执行失败。。。");
            System.out.println("发送消息到MQ！！！！");
            //6.将submit发送到日志topic队列中，让ES保存当前submit信息
            pushMsgUtil.sendLog(standardSubmit, StrategyConstants.STRATEGY_ERROR_BLACK);
            //7.将Standarreport对象封装，发送到状态报告topic队列中，让接口模块给用户推送
            pushMsgUtil.pushReport(standardSubmit,StrategyConstants.STRATEGY_ERROR_BLACK);
            return false;
        }

    }
}
